package com.sc.nft.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.sc.nft.dao.UserMarketValueDao;
import com.sc.nft.dao.UserWorkRoomDao;
import com.sc.nft.entity.ApplicationRequirement;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.ApplicationRequirementFactoryVO;
import com.sc.nft.enums.ApplicationRequirementEnum;
import com.sc.nft.service.ApplicationRequirementFactoryService;
import com.sc.nft.util.DecideUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@RequiredArgsConstructor
@Service
public class TeamAccountServiceImpl implements ApplicationRequirementFactoryService {
    private final UserWorkRoomDao userWorkRoomDao;

    private final UserMarketValueDao userMarketValueDao;
    @Override
    public ApplicationRequirementFactoryVO applicationRequirement(UserInfo userInfo, ApplicationRequirement applicationRequirement, Integer type) {
        // 如果是社区网点,那么旗下的社区网点和城市服务中心包括团队,都要排除掉.如果是申请的是城市服务中心,那么旗下的城市服务中心和团队都要排除掉
        // 这个判定是用来判断,是否要排除社区网点的判定,两者一定排除的是城市服务中心,如果他是社区网点的申请,那么就要把他变成true.用来对他进行一起的排除.
        Boolean exclude = Boolean.FALSE;
        if (type.equals(1)) {
            exclude = Boolean.TRUE;
        }
        // 获取排除社区工作室和城市服务中心的用户
        List<Long> usersWhoExcludeUsersFromCommunityStudiosAndCityServiceCenters = userWorkRoomDao.getUsersWhoExcludeUsersFromCommunityStudiosAndCityServiceCenters(userInfo.getId(), exclude);
        Integer userIdsPositionCount=0;
        String value = applicationRequirement.getValue();
        List<String> split = StrUtil.split(value, ",");
        if (CollUtil.isNotEmpty(usersWhoExcludeUsersFromCommunityStudiosAndCityServiceCenters)) {
            // 获取usersWhoExcludeUsersFromCommunityStudiosAndCityServiceCenters的数据
            userIdsPositionCount= userMarketValueDao.getUserIdsPositionCount(usersWhoExcludeUsersFromCommunityStudiosAndCityServiceCenters, Convert.toBigDecimal(split.get(0)));
        }
        ApplicationRequirementFactoryVO applicationRequirementFactoryVO = new ApplicationRequirementFactoryVO();
        applicationRequirementFactoryVO.setKey(StrUtil.format("{}{}{}", applicationRequirement.getType().getDescription(), applicationRequirement.getJudgingCriteria(), Convert.toInt(split.get(1))));
        applicationRequirementFactoryVO.setSmallCharacter(StrUtil.format("{}{}",applicationRequirement.getType().getDescriptionMinCharacters(),Convert.toInt(split.get(0))));
        applicationRequirementFactoryVO.setAskForValue(split.get(1));
        applicationRequirementFactoryVO.setApplicationRequirementEnum(ApplicationRequirementEnum.TEAM_ACCOUNT);
        applicationRequirementFactoryVO.setPracticalValue(Convert.toInt(userIdsPositionCount));
        applicationRequirementFactoryVO.setPracticalValueStr(Convert.toInt(userIdsPositionCount));
        // 根据compoundMode字段中的 > < >= <= = 五中 来判定是否满足条件
        applicationRequirementFactoryVO.setSatisfy(DecideUtil.conditionalDecision(userIdsPositionCount, Convert.toInt(split.get(1)), applicationRequirement.getJudgingCriteria()));
        return applicationRequirementFactoryVO;

    }
    @Override
    public Integer code() {
        return ApplicationRequirementEnum.TEAM_ACCOUNT.getCode();
    }
}
